/**
 * jims
 */
package com.jims.master.modules.nurse.service;

import java.util.Date;
import java.util.List;

import com.jims.master.common.config.Global;
import com.jims.master.common.utils.DateUtil;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.dao.PatVisitDao;
import com.jims.master.modules.doctor.dao.PatVisitIndexDao;
import com.jims.master.modules.doctor.dao.PatsInTransferringDao;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PatVisitIndex;
import com.jims.master.modules.doctor.entity.PatsInTransferring;
import com.jims.master.modules.nurse.dao.BedRecDao;
import com.jims.master.modules.nurse.entity.BedRec;
import com.jims.master.modules.sys.utils.UserUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.nurse.entity.NewbornRec;
import com.jims.master.modules.nurse.dao.NewbornRecDao;

/**
 * 新生儿记录Service
 * @author 赵宁
 * @version 2017-04-10
 */
@Service
@Transactional(readOnly = true)
public class NewbornRecService extends CrudService<NewbornRecDao, NewbornRec> {
	@Autowired
    private PatMasterIndexDao patMasterIndexDao;
	@Autowired
	private PatVisitDao patVisitDao;
	@Autowired
	private PatVisitIndexDao patVisitIndexDao;
	@Autowired
	private BedRecDao bedRecDao;
	@Autowired
	private PatsInTransferringDao patsInTransferringDao;

	public NewbornRec get(String id) {
		return super.get(id);
	}
	
	public List<NewbornRec> findList(NewbornRec newbornRec) {
		return super.findList(newbornRec);
	}
	
	public Page<NewbornRec> findPage(Page<NewbornRec> page, NewbornRec newbornRec) {
		return super.findPage(page, newbornRec);
	}
	
	@Transactional(readOnly = false)
	public int save(NewbornRec newbornRec) {
		return super.save(newbornRec);
	}
	/**
	 * 查询入院患者新生儿登记列表
	 * @param page
	 * @param newbornRec
	 * @return Page<BaseDto>
	 * @author lizhongyu
	 */
	public Page<BaseDto> findornRecList(Page<BaseDto> page,NewbornRec newbornRec){
		List<BaseDto> list=dao.findornRecList(newbornRec);
		page.setCount(list.size());
		page.setList(list);
		return page;
	}
	/**
	 * 新生儿住院登记
	 * @param newbornRec
	 * @return
	 * @author 赵宁
	 * @update by CTQ at 2017-07-21 20:42:01
	 */
	@Transactional(readOnly = false)
	public int saveInHosInfo(NewbornRec newbornRec) {
		int code=0;
		Date date = new Date();
		if(StringUtils.isNotBlank(newbornRec.getId())){
			/**插入新生儿基本信息**/
			PatMasterIndex patMasterIndex = newbornRec.getPatMasterIndex();
			patMasterIndex.setName(newbornRec.getName());
			patMasterIndex.setDeptId(newbornRec.getPatVisit().getDeptAdmissionTo());
			patMasterIndex.setAge("0");
			patMasterIndex.setBirthdayDate(newbornRec.getDateOfBirth());
			patMasterIndex.updateOrgId();
			patMasterIndex.preInsert();
			code+=patMasterIndexDao.insert(patMasterIndex);
			/**插入新生儿住院信息**/
			PatVisit patVisit =newbornRec.getPatVisit();
			String wardCode= UserUtils.getUser().getOrgDept().getId();
			PatVisit pv = patVisitDao.getPrintInfo(newbornRec.getVistIdOfMother());
			patVisit.setDeptAdmissionTo(pv!=null?pv.getDeptAdmissionTo():"");//默认入院科室新生儿母亲所在科室
			patVisit.setAdmissionDate(date);
			patVisit.setWardAdmissionTo(wardCode);//入院病区
			patVisit.setWardCode(wardCode);
			patVisit.setVisitNo(newbornRec.getBabyInpNo());
			patVisit.updateOrgId();
			patVisit.setInFlag("0");
			patVisit.setPatientId(patMasterIndex.getId());
			patVisit.setDeptFlag("0");
			patVisit.setWardFlag("1");
			//patVisit.setSituationDate(patVisit.getSituationDate());
			patVisit.preInsert();
			patVisit.setBedNo(newbornRec.getBedNo());
			patVisit.setVisitNo(newbornRec.getBabyInpNo());
			code+=patVisitDao.insert(patVisit);
			/**插入新生儿住院基本信息**/
			PatVisitIndex patVisitIndex = new PatVisitIndex();
			patVisitIndex.setName(newbornRec.getName());
			patVisitIndex.setVisitId(patVisit.getId());
			patVisitIndex.updateOrgId();
			patVisitIndex.setAddress(newbornRec.getBirthPlaceProvince());
			patVisitIndex.setAge("0");
			patVisitIndex.setBirthdayDate(newbornRec.getDateOfBirth());
			patVisitIndex.setChargeType(newbornRec.getPatMasterIndex().getChargeType());
			patVisitIndex.setHeight(newbornRec.getHeight());
			patVisitIndex.setIdentity(newbornRec.getPatMasterIndex().getIdentity());
			patVisitIndex.setSex(newbornRec.getSex());
			patVisitIndex.setNewbornFlg(Global.YES);//是新生儿
			patVisitIndex.setWeight(!newbornRec.getWeight().equals("")?newbornRec.getWeight():null);
			patVisitIndex.setHeight(!newbornRec.getHeight().equals("")?newbornRec.getHeight():null);
			patVisitIndex.preInsert();
			code+=patVisitIndexDao.insert(patVisitIndex);

			/**根据母亲信息，查询母亲的床位信息**/
			BedRec momRec = new BedRec();
			momRec.setPatientId(newbornRec.getPatientIdOfMother());
			momRec.setVisitId(newbornRec.getVistIdOfMother());
			momRec.updateOrgId();
			BedRec momBed = bedRecDao.findBedRecByParams(momRec);
			/**判断新生儿是否已插入床位信息，如果没有，插入床位**/
			BedRec br = new BedRec();
			br.setWordCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
			br.setBedLabel(newbornRec.getBedNo());

			br.updateOrgId();
			List<BedRec> bedRecs = bedRecDao.findbedRecNo(br);
			if (bedRecs==null || bedRecs.size()==0){
				/**插入新生儿住院床位信息**/
				int child_no = bedRecDao.findChildBedNo(momBed);
				momBed.setId("");
				momBed.preInsert();
				momBed.setVisitId(patVisit.getId());
				momBed.setPatientId(patMasterIndex.getId());
				momBed.setBedLabel(patVisit.getBedNo());
				//查询母亲下是否已有婴儿住院，处理婴儿住院床号生成规则为母亲床号01，母亲床号02...
				momBed.setBedNo(child_no);
				/*不收新生儿的床位费和空调费*/
				momBed.setBedGrade("");
				momBed.setAirconditionClass("");
				momBed.setBedApprovedType("3");//童床
				momBed.preInsert();
				bedRecDao.insert(momBed);

				/**插入新生儿待入科记录**/
				PatsInTransferring patsInTransferring = new PatsInTransferring();
				patsInTransferring.setPatientId(patMasterIndex.getId());
				patsInTransferring.setVisitId(patVisit.getId());
				patsInTransferring.setDeptTransferTo(patVisit.getDeptAdmissionTo());
				patsInTransferring.setTransferDate(date);
				patsInTransferring.updateOrgId();
				patsInTransferring.preInsert();
				patsInTransferringDao.insert(patsInTransferring);
				/**插入新生儿在病区记录**/


			}
			/**更新新生儿住院记录信息**/
			newbornRec.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
			newbornRec.setPatientId(patMasterIndex.getId());
			newbornRec.setNewpatientId(patMasterIndex.getId());
			newbornRec.setNewvisitId(patVisit.getId());
		}
			newbornRec.preUpdate();
			code+=dao.update(newbornRec);

		return code;
	}

    /**
     * 新生儿住院修改保存
     * @param newbornRec
     * @return
     * @author xueliang
     */
    @Transactional(readOnly = false)
    public int updateNewBornHos(NewbornRec newbornRec) {
        int code=0;
        //基本信息
        PatMasterIndex patMasterIndex = newbornRec.getPatMasterIndex();
        patMasterIndex.setName(newbornRec.getName());
        patMasterIndex.setDeptId(newbornRec.getPatVisit().getDeptAdmissionTo());
        patMasterIndex.setAge("0");
        patMasterIndex.setBirthdayDate(newbornRec.getDateOfBirth());
        patMasterIndex.setOrgId(UserUtils.getOrgId());
        patMasterIndex.preInsert();
        code=patMasterIndexDao.insert(patMasterIndex);
        //住院信息
        PatVisit patVisit =newbornRec.getPatVisit();
        String wardCode= UserUtils.getUser().getOrgDept().getId();
        patVisit.setWardCode(wardCode);
        patVisit.setVisitNo(newbornRec.getBabyInpNo());
        patVisit.setOrgId(UserUtils.getOrgId());
        patVisit.setInFlag("0");
        patVisit.setPatientId(patMasterIndex.getId());
        patVisit.setDeptFlag("1");
        patVisit.setWardFlag("1");
        patVisit.preInsert();
        patVisit.setBedNo(newbornRec.getBedNo());
        patVisit.setVisitNo(newbornRec.getMotherInpNo());
        patVisitDao.insert(patVisit);
        //住院索引
        PatVisitIndex patVisitIndex = new PatVisitIndex();
        patVisitIndex.setName(newbornRec.getName());
        patVisitIndex.setVisitId(patVisit.getId());
        patVisitIndex.setOrgId(UserUtils.getOrgId());
        patVisitIndex.setAddress(newbornRec.getBirthPlaceProvince());
        patVisitIndex.setAge("0");
        patVisitIndex.setBirthdayDate(newbornRec.getDateOfBirth());
        patVisitIndex.setChargeType(newbornRec.getPatMasterIndex().getChargeType());
        patVisitIndex.setHeight(newbornRec.getHeight());
        patVisitIndex.setIdentity(newbornRec.getPatMasterIndex().getIdentity());
        patVisitIndex.setSex(newbornRec.getSex());
        patVisitIndex.setWeight(newbornRec.getWeight());
        patVisitIndex.preInsert();
        patVisitIndexDao.insert(patVisitIndex);

	/*	BedRec bedRec = newbornRec.getBedRec();
		bedRec.setPatientId(patMasterIndex.getId());
		bedRec.setOrgId(UserUtils.getOrgId());
		bedRec.setVisitId(patVisit.getId());*/

        return code;
    }




    /**
	 * 新生儿登记==查询病人列表
	 * @param newbornRec
	 * @return Page<BaseDto>
	 * @author 赵宁
	 */
	public Page<BaseDto> getPatientList(Page<BaseDto> page,NewbornRec newbornRec){

		List<BaseDto> list=dao.getPatientList(newbornRec);
		page.setList(list);
		page.setCount(list.size());
		return page;
	}

	/**
	 * 查询新生儿列表
	 * @param page
	 * @param newbornRec
	 * @return Page<BaseDto>
	 * @author 赵宁
	 */
	public Page<BaseDto> getNewBornList(Page<BaseDto> page,NewbornRec newbornRec){
		 List<BaseDto> list=dao.getNewBornList(newbornRec);
		 page.setCount(list.size());
		 page.setList(list);
		 return page;
	}

    /**
     * 查询新生儿列表
     * @param newbornRec
     * @author xueliang
     */
    public Page<NewbornRec> findListByVistIdOfMother(Page<NewbornRec> page,NewbornRec newbornRec){
        List<NewbornRec> list=dao.findListByVistIdOfMother(newbornRec);
        page.setCount(list.size());
        page.setList(list);
        return page;
    }


    /**
     * 查询新生儿住院列表
     * @param newbornRec
     * @author xueliang
     */
    public Page<NewbornRec> findNewBornInHosList(Page<NewbornRec> page,NewbornRec newbornRec){
        List<NewbornRec> list=dao.findNewBornInHosList(newbornRec);
        page.setCount(list.size());
        page.setList(list);
        return page;
    }



    /**
     * 查询新生儿住院信息修改回显
     * @param newbornRec
     * @return List<BaseDto>
     * @author xueliang
     */

    public BaseDto findNewBornInHos(NewbornRec newbornRec){
		List<NewbornRec> list= dao.findListByVistIdOfMother(newbornRec);
		BaseDto baseDto = dao.findNewBornInHos(newbornRec);
		if(list.size() != 0) {
			int size = list.size() + 1;
			String babyInpNo = list.get(0).getBabyInpNo();
			String babyInpNo2 = babyInpNo.substring(0, babyInpNo.length()>0?babyInpNo.length() - 1:0) + size;
			baseDto.put("babyInpNo",babyInpNo2);
		}else{
			baseDto.put("babyInpNo",baseDto.getAsString("babyInpNo")+"_1");
		}
        return baseDto;
    }


    /**
     * 新生儿住院登记-自动补全功能
     * @param q
     * @param newbornRec
     * @param limit
     * @return
     * @author CTQ
     * @date 2017-7-20 20:04:002
     */
    public List<NewbornRec> findListByParam(String q,NewbornRec newbornRec,String limit){
        newbornRec.updateOrgId();
        List<NewbornRec> list=  dao.findListByParam(q,newbornRec,limit);
        return list;
    }

	/**
	 * 根据新生儿母亲信息，处理新生儿信息
	 * @param newbornRec
	 * @return
	 * @author CTQ
	 * @date 2017-07-21 17:17:43
	 */
	public NewbornRec findNewBornInfo(NewbornRec newbornRec){
		List<NewbornRec> list= dao.findListByVistIdOfMother(newbornRec);
		if(list.size() != 0){
			//int size= list.size()+1;
			//String babyInpNo= list.get(0).getBabyInpNo();
			//String babyInpNo2=  babyInpNo.substring(0,babyInpNo.length()>0?babyInpNo.length()-1:0)+size;
			list.get(0).setName( list.get(0).getNameOfMother()+"_女");
			//list.get(0).setBabyInpNo(babyInpNo2);
			list.get(0).setId("");
			newbornRec = list.get(0);
		}else{
			PatMasterIndex patMasterIndex  = new PatMasterIndex();
			if(StringUtils.isNotBlank(newbornRec.getPatientIdOfMother())){
				patMasterIndex= patMasterIndexDao.get(newbornRec.getPatientIdOfMother());
				//newbornRec.setBabyInpNo(newbornRec.getMotherInpNo()+"_1");
				newbornRec.setName(patMasterIndex.getName()+"_女");
				newbornRec.setAgeOfMother(patMasterIndex.getAge());
				newbornRec.setCitizenshipOfMother(patMasterIndex.getNationality());
				newbornRec.setNameOfMother(patMasterIndex.getName());
				newbornRec.setNationOfMother(patMasterIndex.getNation());
				newbornRec.setIdNoOfMother(patMasterIndex.getIdNo());
				newbornRec.setNameOfFacility(UserUtils.getOrgId());
				newbornRec.setDateOfIssue(new Date());
				newbornRec.setDateOfBirth(new Date());
			}
		}
		return newbornRec;

	}
}